/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.sld2owl.owl;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;

public class RolePartTranslator {

    /**
     * Creates a new instance of RolePartTranslator    
     * @param lnode The role part node of the Dom structure
     * @param ont The ontology
     * @param instAct The activity instance
     * @param nsName The namespace
     */
    public RolePartTranslator(Node lnode, OntModel ont, Individual instAct,
            String nsName) {

        OntClass rolePart =
                ont.getOntClass(nsName + "Role-Part");
        OntClass learningActivity =
                ont.getOntClass(nsName + "Learning-Activity");
        OntClass activityStructure =
                ont.getOntClass(nsName + "Activity-Structure");
        OntClass environment =
                ont.getOntClass(nsName + "Environment");

        DatatypeProperty identifier =
                ont.getDatatypeProperty(nsName + "identifier");
        DatatypeProperty title =
                ont.getDatatypeProperty(nsName + "title");
        ObjectProperty roleRef =
                ont.getObjectProperty(nsName + "role-ref");
        ObjectProperty environmentRef =
                ont.getObjectProperty(nsName + "environment-ref");
        ObjectProperty learningActivityRef =
                ont.getObjectProperty(nsName + "learning-activity-ref");
        ObjectProperty supportActivityRef =
                ont.getObjectProperty(nsName + "support-activity-ref");
        ObjectProperty activityStructureRef =
                ont.getObjectProperty(nsName + "activity-structure-ref");
        ObjectProperty unitOfLearningRef =
                ont.getObjectProperty(nsName + "unit-of-learning-ref");


        try {


            NodeList rpChildren =
                    lnode.getChildNodes();
            NamedNodeMap attrs =
                    lnode.getAttributes();

            if (rpChildren != null) {
                lastInstanceSLDTranslator lastInst =
                        new lastInstanceSLDTranslator();
                String instance =
                        "role_part_instance_" +
                        (String.valueOf(lastInst.lastInstance(rolePart)));

                Individual instLd =
                        ont.createIndividual(nsName + instance, rolePart);

                OntClass act =
                        ont.getOntClass(nsName + "Act");

                ObjectProperty rolePartRef =
                        ont.getObjectProperty(nsName + "role-part-ref");
                instAct.setPropertyValue(rolePartRef, instLd);

                for (int y =
                        0; y < rpChildren.getLength(); y++) {

                    if (rpChildren.item(y).getNodeName().
                            compareTo("imsld:title") == 0) {
                        //   String ltitle = rpChildren.item(y).getNodeValue();
                        String ltitle =
                                rpChildren.item(y).getChildNodes().item(0).
                                getNodeValue();
                        if (ltitle != null) {
                            RDFNode titleValue =
                                    ont.createTypedLiteral(ltitle);
                            instLd.setPropertyValue(title, titleValue);

                        }
                        //Add the attribute identifier
                        for (int a =
                                0; a < attrs.getLength(); a++) {
                            Node attr =
                                    attrs.item(a);
                            if (attr.getNodeName().trim().compareTo("identifier") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode value =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLd.setPropertyValue(identifier, value);
                            }



                        }
                    }
                }
                for (int y =
                        0; y < rpChildren.getLength(); y++) {
                    if (rpChildren.item(y).getNodeName().
                            compareTo("imsld:role-ref") == 0) {
                        NamedNodeMap atttr =
                                rpChildren.item(y).getAttributes();
                        for (int a =
                                0; a < atttr.getLength(); a++) {
                            Node att =
                                    atttr.item(a);
                            if (att.getNodeName().trim().compareTo("ref") == 0) {
                                RDFNode roleRefValue =
                                        ont.createTypedLiteral(att.getNodeValue().
                                        trim());
                                instLd.setPropertyValue(roleRef, roleRefValue);
                            }


                        }
                    }
                    if (rpChildren.item(y).getNodeName().compareTo(
                            "imsld:learning-activity-ref") == 0) {
                        NamedNodeMap atttr =
                                rpChildren.item(y).getAttributes();
                        for (int a =
                                0; a < atttr.getLength(); a++) {
                            Node att =
                                    atttr.item(a);
                            if (att.getNodeName().trim().compareTo("ref") == 0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode learningActivityValue =
                                        ont.createTypedLiteral(att.getNodeValue().
                                        trim(), overType);
                                searchInstance searchInst =
                                        new searchInstance();
                                Individual instLearningActivity =
                                        searchInst.search(learningActivity,
                                        identifier, learningActivityValue);
                                instLd.setPropertyValue(learningActivityRef,
                                        instLearningActivity);
                            }
                        }
                    }
                    if (rpChildren.item(y).getNodeName().compareTo(
                            "imsld:support-activity-ref") == 0) {
                        NamedNodeMap atttr =
                                rpChildren.item(y).getAttributes();
                        for (int a =
                                0; a < atttr.getLength(); a++) {
                            Node att =
                                    atttr.item(a);
                            if (att.getNodeName().trim().compareTo("ref") == 0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode supportActivityValue =
                                        ont.createTypedLiteral(att.getNodeValue().
                                        trim(), overType);
                                searchInstance searchInst =
                                        new searchInstance();
                                Individual instSupportActivity =
                                        searchInst.search(learningActivity,
                                        identifier, supportActivityValue);
                                instLd.setPropertyValue(supportActivityRef,
                                        instSupportActivity);
                            }

                        }
                    }
                    if (rpChildren.item(y).getNodeName().compareTo(
                            "imsld:activity-structure-ref") == 0) {
                        NamedNodeMap atttr =
                                rpChildren.item(y).getAttributes();
                        for (int a =
                                0; a < atttr.getLength(); a++) {
                            Node att =
                                    atttr.item(a);
                            if (att.getNodeName().trim().compareTo("ref") == 0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode activityStructureValue =
                                        ont.createTypedLiteral(att.getNodeValue().
                                        trim(), overType);
                                searchInstance searchInst =
                                        new searchInstance();
                                Individual instActivityStructure =
                                        searchInst.search(activityStructure,
                                        identifier, activityStructureValue);
                                instLd.setPropertyValue(activityStructureRef,
                                        instActivityStructure);
                            }

                        }
                    }
                    if (rpChildren.item(y).getNodeName().compareTo(
                            "imsld:unit-of-learning-href") == 0) {
                        NamedNodeMap atttr =
                                rpChildren.item(y).getAttributes();
                        for (int a =
                                0; a < atttr.getLength(); a++) {
                            Node att =
                                    atttr.item(a);
                            if (att.getNodeName().trim().compareTo("ref") == 0) {

                                RDFNode unitOfLearningValue =
                                        ont.createTypedLiteral(att.getNodeValue().
                                        trim());
                                instLd.setPropertyValue(unitOfLearningRef,
                                        unitOfLearningValue);

                            }

                        }
                    }
                    if (rpChildren.item(y).getNodeName().
                            compareTo("imsld:environment-ref") == 0) {
                        NamedNodeMap atttr =
                                rpChildren.item(y).getAttributes();
                        for (int a =
                                0; a < atttr.getLength(); a++) {
                            Node att =
                                    atttr.item(a);
                            if (att.getNodeName().trim().compareTo("ref") == 0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode environmentRefValue =
                                        ont.createTypedLiteral(att.getNodeValue().
                                        trim(), overType);
                                searchInstance searchInst =
                                        new searchInstance();
                                Individual instEnvironment =
                                        searchInst.search(environment,
                                        identifier, environmentRefValue);
                                instLd.setPropertyValue(environmentRef,
                                        instEnvironment);
                            }

                        }
                    }
                }//for   
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
